import { defineStore } from 'pinia';
import type { RouteRecordNormalized } from 'vue-router';
import defaultSettings from '@/config/settings.json';
import { AppState } from '@/types'
import { getStorage, setStorage } from '@/utils/tools'
import { getEnumApi, getSystemInfo } from '@/api/common'


export const configStore = defineStore({
  id: 'app-config',
  state: () => ({
    pageLimit: getStorage('config_page_limit'),
    enum: getStorage('enum'),
    systemInfo: getStorage('system_info'),
  }),
  actions: {
    setPageLimit(value: any) {
      setStorage('config_page_limit', value);
      this.$patch({
        pageLimit: value
      })
    },
    async setSystemInfo() {
      try {
        const { data }: any = await getSystemInfo()
        setStorage('system_info', data);
        this.$patch({
          systemInfo: data
        })
        return data
      } catch (e) {
        return false;
      }
    },
    async setEnum() {
      try {
        const { data }: any = await getEnumApi()
        this.$patch({
          enum: data
        })
        return data
      } catch (e) {
        return false;
      }
    },
    getEnumItem(itemKey: any): any {
      try {
        return this.enum[itemKey]
      } catch (e) {
        return []
      }
    },
  },
  getters: {
    getPageLimit(state): any {
      return state.pageLimit == null ? 20 : state.pageLimit
    },
    getEnum(state): any {
      return state.enum
    },
    getSystemInfo(state): any {
      return state.systemInfo
    },
  }
})


export default configStore;